package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BankBean;
import com.koron.css2.baseConfig.mapper.BankMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 根据客户查询账户
 * 
 * @author 甄玉晴 2020年4月7日
 */
public class QuerySameAccount implements ServerInterface {

	@SuppressWarnings("rawtypes")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			// 入参转换
			CtmInfoBean bean = JsonUtils.objectToPojo(req.getData(), CtmInfoBean.class);
			if (StringUtils.isBlank(bean.getCtmNo())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "客户编号不能为空。", Void.class);
			}
			AccountInfoMapper mapper = factory.getMapper(AccountInfoMapper.class);
			List<AccountInfoBean> list = new ArrayList<AccountInfoBean>();
			// 根据客户编号查询账户列表
			list = mapper.getAccountInfoByCtmNo(bean.getCtmNo(), AccountInfoBean.ACCOUNT);
			
			//账号信息
			Map<String, String> bpw = Tools.mapDicByCode(factory, "BPW");//缴费方式
			Map<String, String> act = Tools.mapDicByCode(factory, "ACT");//账户类型
			BankMapper bankeMapper = factory.getMapper(BankMapper.class);
			// 获取开户行名称
			if(list!=null && list.size()>0) {
				for(AccountInfoBean account:list) {
					account.setAccountTypeName(act.get(account.getAccountType()));
					account.setPayWayName(bpw.get(account.getPayWay()));
					if (!StringUtils.isBlank(account.getOpenAccountBank())) {
						BankBean banke = bankeMapper.findBankById(account.getOpenAccountBank());
						account.setOpenAccountBankName(banke==null?"":banke.getBankName());

					}
					if(StringUtils.isNotBlank(account.getVatFlag()) && "1".equals(account.getVatFlag())) {
						account.setVatFlagName("是");
					}else {
						account.setVatFlagName("否");
					}
					// 获取购买方开户行名称
					if (!StringUtils.isBlank(account.getInvoiceBank())) {
						BankBean banke = bankeMapper.findBankById(account.getInvoiceBank());
						account.setInvoiceBankName(banke==null?"":banke.getBankName());
					}
					//投递方式
					if (account != null && StringUtils.isNotBlank(account.getDeliverWay())) {
						account.setDeliverWayName(Tools.getDicNameByCodeAndValue(factory, "SDW", account.getDeliverWay()));
					}
					if (account != null && StringUtils.isNotBlank(account.getInvoiceType())) {
						account.setInvoiceTypeName(Tools.getDicNameByCodeAndValue(factory, "BBT", account.getInvoiceType()));
					}
				}
				
			}
			
			
			// 返回结果集
			MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "sucess", List.class);
			msg.setData(list);
			return msg;
		} catch (Exception e) {
			logger.error("根据客户编号查询账户查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", Void.class);
		}
	}

}
